﻿Imports System.Data.SqlClient
Imports System.IO
Public Class BACKUP

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        FazerBackup()
        Cursor.Current = Cursors.WaitCursor
    End Sub
    Public Sub FazerBackup()
        Dim caminhobackup As String = String.Format("{0}\BACKUP\{1}.BAK", "\\SERVIDOR1\Publico Administrativo\Público Administrativo\FUNIL DE VENDAS", FormatDateTime(Date.Today, DateFormat.ShortDate).Replace("/", "").ToString)
        If Not Directory.Exists("\\SERVIDOR1\Publico Administrativo\Público Administrativo\FUNIL DE VENDAS" & "\BACKUP\") Then
            Directory.CreateDirectory("\\SERVIDOR1\Publico Administrativo\Público Administrativo\FUNIL DE VENDAS" & "\BACKUP\")
        End If
        Dim conexao As New SqlConnection(My.Settings.FUNILConnectionString)
        Dim comando As New SqlCommand()
        comando.Connection = conexao
        Try
            comando.CommandText = "BACKUP DATABASE [FUNIL3] TO DISK = '" & caminhobackup & "'"
            conexao.Open()
            comando.ExecuteNonQuery()
            conexao.Close()
            Cursor.Current = Cursors.Default
            MsgBox("Backup Realizado com Sucesso.", MsgBoxStyle.Information)

        Catch ex As Exception
            MsgBox("Erro ao fazer cópia de segurança.Tente novamente." + ex.ToString, MsgBoxStyle.Information, "Erro")
        End Try
    End Sub
    Private Sub BACKUP_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        TextBox1.Text = My.Settings.FUNILConnectionString
        TextBox2.Text = "\\SERVIDOR1\Publico Administrativo\Público Administrativo\FUNIL DE VENDAS" & "\BACKUP\"
    End Sub
End Class